<template>
	<view>
		<u-form :model="form" ref="uForm" style="padding:0 30rpx;" label-width="220">
			<u-form-item label="套餐">
				<view class="formItem">{{data.name}}</view>
			</u-form-item>

			<u-form-item label="开放商品位数量">
				<view class="formItem">{{'+'+data.businessNum}}</view>
			</u-form-item>

			<u-form-item label="时长">
				<view class="formItem">{{'+'+time[data.type]}}</view>
			</u-form-item>

			<u-form-item label="价格">
				<view class="formItem">{{data.sweet+'糖果'}}</view>
			</u-form-item>

			<u-form-item :label="data.needSweet>=0?'支付糖果':'退还糖果'" v-if="data.status == 2">
				<view class="formItem">{{data.needSweet>=0?data.needSweet+'糖果':Math.abs(data.needSweet)+'糖果'}}</view>
			</u-form-item>

			<u-form-item label="支付糖果" v-else>
				<view class="formItem">{{data.sweet+'糖果'}}</view>
			</u-form-item>

			<u-form-item>
				<view style="padding-right:30rpx;">密码</view>
				<u-input v-model="form.password" type="password" placeholder="请输入你的支付密码" />
			</u-form-item>

			<view style="padding:30rpx 0;">购买须知</view>
			<view style="color: #888;" v-for="(item,index) in data.notes.split('\n')" :key="index">{{item}}</view>
		</u-form>

		<view class="btn">
			<u-button type="primary" class="btnSubmit" @click="confirm">立即{{this.btnText[this.data.status]}}</u-button>
		</view>
	</view>
</template>

<script>
	import md5Libs from "uview-ui/libs/function/md5";
	export default {
		data() {
			return {
				show: false,
				data: {},
				btnText: {
					0: '购买',
					1: '续期',
					2: '升级',
					3: '购买'
				},
				time: {
					1: '30',
					2: '90',
					3: '365'
				},
				form: {
					password: ''
				}

			}
		},
		onLoad(option) {
			this.data = JSON.parse(option.item);
			uni.setNavigationBarTitle({
				title: this.btnText[this.data.status] + this.data.desc.split('\n')[0]
			});
		},
		methods: {
			confirm() {
				if (!this.form.password) {
					this.$u.toast('请输入密码')
					return false
				}
				this.$u.offlineApi.comboBuy({
					id: this.data.id,
					password: md5Libs.md5(this.form.password)
				}).then(res => {
					if (res.code == 0) {
						this.$u.toast(this.btnText[this.data.status] + '成功！');
						this.back()
					} else {
						this.$u.toast(this.btnText[this.data.status] + '失败！' + res.msg);
					}
				})
			},
			back() {
				uni.navigateBack({
					delta: 2
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.btn {
		display: flex;
		justify-content: center;
		padding: 60rpx 0;

		.btnSubmit {
			width: 80%;
			background: #357cd4;
		}
	}

	.formItem {
		display: flex;
		justify-content: flex-end;
		width: 100%;
		color: #888;
	}
</style>
